//
//  ACEStudyTopView.m
//  ACE
//
//  Created by zhangtao on 2018/8/8.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACEStudyTopView.h"
#import "UIButton+ACEBadge.h"

@implementation ACEStudyTopView
{
    NSMutableArray <UIButton *>*btns;
    UIButton *middleButton;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        btns = [NSMutableArray arrayWithCapacity:0];
        
        
        NSArray *titles = @[@"全部",@"代付款",@"进行中",@"待确认结束"];
        CGFloat space = 10;
        CGFloat buttonW = (frame.size.width-space*(titles.count+1))/titles.count;
        for (NSInteger i = 0; i<titles.count; i++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0+(space+buttonW)*i, 10, buttonW,frame.size.height-10)];
            [btn setBackgroundColor:[UIColor clearColor]];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            btn.selected = NO;
            [btn setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:17];
            [self addSubview:btn];
            [btn setTag:i];
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            
//            CGPoint centerPoint = btn.center;
            [btn sizeToFit];
            [btn showBadgeWithNum:6];

//            btn.center = centerPoint;
            [btns addObject:btn];
            
            if (i == 0) {
                _markLab = [[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x, frame.size.height-4, btn.frame.size.width, 2)];
                _markLab.backgroundColor = deepGreen;
                [self addSubview:_markLab];
            }
            
        }
        
       
    }
    return self;
}

-(void)click:(UIButton *)btn{
    
    if (btn == middleButton) {
        return;
    }
    
    if (middleButton) {
        [middleButton showNormalStateWithFont:17];
        middleButton.selected = NO;
    }
    
    if (btn.selected == NO) {
        [btn showBigStateWithFont:20];
        btn.selected = YES;
    }else{
        [btn showNormalStateWithFont:17];
        btn.selected = NO;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.markLab.frame = CGRectMake(btn.frame.origin.x, self.frame.size.height-4, btn.frame.size.width, 2);

    });
    middleButton = btn;

}

@end


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
